package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.mapper.BaseAttrInfoMapper;
import com.atguigu.gmall.product.mapper.BaseAttrValueMapper;
import com.atguigu.gmall.product.model.BaseAttrInfo;
import com.atguigu.gmall.product.model.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: atguigu
 * @create: 2023-02-20 16:11
 */
@Service
public class BaseAttrInfoServiceImpl extends ServiceImpl<BaseAttrInfoMapper, BaseAttrInfo> implements BaseAttrInfoService {


    @Autowired
    private BaseAttrValueService baseAttrValueService;

    //@Autowired
    //private BaseAttrValueMapper baseAttrValueMapper;

    /**
     * 根据一,二,三级分类ID查询平台属性以及平台属性值
     *
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseAttrInfo> getAttrInfoList(Long category1Id, Long category2Id, Long category3Id) {
        //方式二:先查询平台属性名称集合
        //遍历平台属性名称 分别 查询每个平台属性包含属性值 手动set设值


        //方式一:调用持久层方法实现平台属性以及值获取-自定义SQL编写(一对多)
        return this.getBaseMapper().getAttrInfoList(category1Id, category2Id, category3Id);

    }

    /**
     * 保存平台属性以及平台属性值
     *
     * @param baseAttrInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {
        if (baseAttrInfo.getId() == null) {
            //2.保存平台属性记录-保存后MyBatisPlus会给实体类中主键赋值
            //1.业务校验-查询该分类下是否已有当前平台属性名称
            //LambdaQueryWrapper<BaseAttrInfo> queryWrapper = new LambdaQueryWrapper<>();
            //queryWrapper.eq(BaseAttrInfo::getAttrName, baseAttrInfo.getAttrName());
            //queryWrapper.eq(BaseAttrInfo::getCategoryId, baseAttrInfo.getCategoryId());
            //queryWrapper.eq(BaseAttrInfo::getCategoryLevel, baseAttrInfo.getCategoryLevel());
            //int count = this.count(queryWrapper);
            //if (count > 0) {
            //    throw new RuntimeException("该分类下已有该平台属性!");
            //}
            this.save(baseAttrInfo);
        } else {
            //修改
            this.updateById(baseAttrInfo);
        }

        //3.批量保存平台属性值
        //3.1 先根据平台属性值列表 将"旧数据"删除掉
        LambdaQueryWrapper<BaseAttrValue> attrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        attrValueLambdaQueryWrapper.eq(BaseAttrValue::getAttrId, baseAttrInfo.getId());
        List<BaseAttrValue> list = baseAttrValueService.list(attrValueLambdaQueryWrapper);


        if (!CollectionUtils.isEmpty(list)) {
            List<Long> attrValIds = list.stream().map(BaseAttrValue::getId).collect(Collectors.toList());
            baseAttrValueService.removeByIds(attrValIds);
        }

        //3.2 按最新提交的平台属性值为准-进行批量保存
        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
        if (!CollectionUtils.isEmpty(attrValueList)) {
            //遍历平台属性值集合,为集合中每个对象关联平台属性
            attrValueList.stream().forEach(attrVal -> {
                //将平台属性值关联到平台属性
                attrVal.setAttrId(baseAttrInfo.getId());
            });
            baseAttrValueService.saveBatch(attrValueList);
        }
    }
}
